<?php
class PhanloaicbsController extends AppController {

	var $name = 'Phanloaicbs';
    var $uses = array('Phanloaicb','Xeploai','Z1plcb','Z1dvutcb','Chidoansv','Xeploaicd','Loaicdcb');
    
	function index() {
        $namhoc_id = $this->Session->read('namhocht_id');
        $chidoan_id=$this->Session->read('chidoanht_id');
		$phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id)));
        $xeploais=$this->Xeploai->find('list',array('fields'=>'tenloai'));
        $loaicdsv=$this->Xeploaicd->find('list',array('fields'=>'loaicd'));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1)$xs++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==4)$y++;
        }
        $tong=count($phanloaicbs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicd=null;
        $check=$this->Loaicdcb->find('first',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id),'recursive'=>-1));
        if($check!=null){
            $loaicd=$check['Loaicdcb']['xeploaicd_id'];    
        }
        $this->set(compact('xeploais','phanloaicbs','loaicdsv','loaicd','tlxs','tlk','tltb','tly','loaicd'));
	}
    function phanloai(){
        //pr($this->data);exit;
        for($j=0;$j<count($this->data['Phanloaicb'])/2;$j++){
            $this->Phanloaicb->updateAll(array('Phanloaicb.xeploai_id'=>$this->data['Phanloaicb']['xeploai_id'.$j]),array('Phanloaicb.id'=>$this->data['Phanloaicb']['id'.$j],'Phanloaicb.duyet'=>0));
        }
        $this->redirect(array('action'=>'index'));
    }
    
    function danhsachcd(){
        $khoa_id=$this->Session->read('khoa_id');
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id'=>null,'khoa_id'=>$khoa_id)));
        $this->set(compact('chidoancbs'));
    }
    
    function duyetcd($cdid=null){
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z1plcb->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $namhoc_id=$this->Session->read('namhocht_id');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1)$xs++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==4)$y++;
        }
        $tong=count($phanloaicbs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd')));
        $check=$this->Loaicdcb->find('first',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdcb']['xeploaicd_id'];
        $this->loadModel('Namhoc');
        $namhocs=$this->Namhoc->find('list',array('fields'=>array('namhoc'),'recursive'=>-1));
        $this->set(compact('phanloaicbs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','namhocs'));
        
        //dvut cbs
        $namhoc_id=$this->Session->read('namhocht_id');
       $dvutcbs=$this->Z1dvutcb->find('all', array('conditions'=> array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$cdid)));
       // pr($dvutsvs);
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutcbs','chidoan'));        
    }
    
    function thongke($namhoc=null,$cdid=null){
        $this->layout='search';
        $this->loadModel('Namhoc');
        $namhoc=$this->Namhoc->find('first',array('conditions'=>array('namhoc'=>$namhoc),'recursive'=>-1));//pr($hk);exit;
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('phanloaicbs','namhoc'));
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z1plcb->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
//        $namhoc_id=$this->Session->read('namhocht_id');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1)$xs++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==4)$y++;
        }
        $tong=count($phanloaicbs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd')));
        $check=$this->Loaicdcb->find('first',array('conditions'=>array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdcb']['xeploaicd_id'];
        $namhocs=$this->Namhoc->find('all',array('fields'=>array('namhoc'),'recursive'=>-1));
        $dvutcbs=$this->Z1dvutcb->find('all', array('conditions'=> array('namhoc_id'=>$namhoc['Namhoc']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('phanloaicbs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','namhocs','dvutcbs'));
    }
    function ketquapldv(){
        $this->layout='excel';
        $namhoc_id = $this->Session->read('namhocht_id');
        $chidoan_id=$this->Session->read('chidoanht_id');
        $this->Z1plcb->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        //$hk=$this->getHocKyHt();
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1)$xs++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
             else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaicbs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $dvutcbs=$this->Z1dvutcb->find('all', array('conditions'=> array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id)));        
        $loaicdcb=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdcb->find('first',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id),'recursive'=>-1));
        $loaicd=$check['Loaicdcb']['xeploaicd_id'];
        $this->set(compact('phanloaicbs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdcb','dvutcbs'));        
    }
    
    function duyet(){
        $namhoc_id=$this->Session->read('namhocht_id');
        $cd=$this->data['Phanloaicb']['chidoan_id'];
        //pr($this->data);exit;
        $chuaduyetids = array(); 
        if(!empty($this->data['Phanloaicb']['chuaduyet'])){             
		foreach($this->data['Phanloaicb']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
    	}
        $duyetids = array();
        if(!empty($this->data['Phanloaicb']['duyet'])){
        foreach($this->data['Phanloaicb']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}}
		if (count($duyetids) == 0 && count($chuaduyetids) == 0  ) {
			$this->Session->setFlash(__('Không có sinh viên nào được chọn', true),'error');
			$this->redirect(array('action'=>'duyetcd',$cd));
		}else {
            $this->Loaicdcb->updateAll(array('Loaicdcb.duyet'=>1),array('Loaicdcb.chidoansv_id'=>$cd,'Loaicdcb.namhoc_id'=>$namhoc_id));
            $this->Phanloaicb->updateAll(array('Phanloaicb.duyet'=>1),array('Phanloaicb.canbo_id'=>$duyetids));
            $this->Phanloaicb->updateAll(array('Phanloaicb.duyet'=>0),array('Phanloaicb.canbo_id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã lưu !', true));
			$this->redirect(array('action'=>'duyetcd',$cd));
		}
    }
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phanloaicb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phanloaicb', $this->Phanloaicb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phanloaicb->create();
			if ($this->Phanloaicb->save($this->data)) {
				$this->Session->setFlash(__('The phanloaicb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanloaicb could not be saved. Please, try again.', true));
			}
		}
		$xeploais = $this->Phanloaicb->Xeploai->find('list');
		$canbos = $this->Phanloaicb->Canbo->find('list');
		$namhocs = $this->Phanloaicb->Namhoc->find('list');
		$this->set(compact('xeploais', 'canbos', 'namhocs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid phanloaicb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phanloaicb->save($this->data)) {
				$this->Session->setFlash(__('The phanloaicb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanloaicb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phanloaicb->read(null, $id);
		}
		$xeploais = $this->Phanloaicb->Xeploai->find('list');
		$canbos = $this->Phanloaicb->Canbo->find('list');
		$namhocs = $this->Phanloaicb->Namhoc->find('list');
		$this->set(compact('xeploais', 'canbos', 'namhocs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for phanloaicb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phanloaicb->delete($id)) {
			$this->Session->setFlash(__('Phanloaicb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Phanloaicb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
